home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / world / wave / wave.java < prev    next >
Text File  |  1996-10-18  |  5KB  |  159 lines

  1. // "Wave Effect"
  2. //     created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
  3. //
  4. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  5. //
  6.  
  7. import vrml.*;
  8. import vrml.node.*;
  9. import vrml.field.*;
  10.  
  11. public class wave extends Script {
  12.     private SFFloat getFraction;
  13.     private float fraction;
  14.  
  15.     private Node coord;
  16.     private int type;
  17.     private float amplitude;
  18.     private float frequency;
  19.     private float[] origin = new float[2];
  20.     private float[] direction = new float[2];
  21.     private boolean set_flag;
  22.  
  23.     private MFVec3f point;
  24.     private int n, i;
  25.     private float p[] = null, y[] = null;
  26.  
  27.     private float d;
  28.     private float PI = 3.14f;
  29.  
  30.     // constructor
  31.     public void initialize() {
  32.         try {
  33.             getFraction = (SFFloat)getEventIn("fraction");
  34.         
  35.             // get field values of script node
  36.             coord = (Node)((SFNode)getField("coord")).getValue();
  37.             type = (int)((SFInt32)getField("type")).getValue();
  38.             amplitude = (float)((SFFloat)getField("amplitude")).getValue();
  39.             frequency = (float)((SFFloat)getField("frequency")).getValue();
  40.             set_flag = (boolean)((SFBool)getField("set_flag")).getValue();
  41.  
  42.             switch (type) {
  43.               default:
  44.               case 0:
  45.                 ((SFVec2f)getField("origin")).getValue(origin);
  46.                 break;
  47.               case 1:
  48.                 ((SFVec2f)getField("direction")).getValue(direction);
  49.                 if (0.0f == direction[0] && 0.0f == direction[1]) {
  50.                     direction[0] = 1.0f;  direction[1] = 0.0f;
  51.                 } else {
  52.                     // normalize direction
  53.                     d = (float)java.lang.Math.
  54.                         sqrt(direction[0] * direction[0] +
  55.                              direction[1] * direction[1]);
  56.                     direction[0] /= d;  direction[1] /= d;
  57.                 }
  58.                 break;
  59.             }
  60.             
  61.             // get vertices of coord
  62.             point = (MFVec3f)coord.getExposedField("point");
  63.             n = point.getSize();
  64.             p = new float[n*3];
  65.             point.getValue(p);
  66.             
  67.             // save initial Y value of vertices
  68.             if (true == set_flag) {
  69.                 y = new float[n];
  70.                 for (i=0; i<n; ++i) {
  71.                     y[i] = p[i*3+1];
  72.                 }
  73.             }
  74.             
  75.         } catch(Exception e) {
  76.             System.out.println("initialize error");
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.     
  81.     public void processEvent(Event e) {
  82.         if (e.getName().equals("fraction")) {
  83.             // get current position
  84.             point.getValue(p);
  85.             
  86.             // set initial Y value if set_flag is TRUE
  87.             if (true == set_flag) {
  88.                 for (i=0; i<n; ++i) {
  89.                     p[i*3+1] = y[i];
  90.                 }
  91.             }
  92.             
  93.             fraction = (2.0f * PI) * getFraction.getValue();
  94.             switch (type) {
  95.               default:
  96.               case 0:
  97.                 for (i=0; i<n; ++i) {
  98.                     d = (float)java.lang.Math.
  99.                         sqrt((p[i*3] - origin[0]) * (p[i*3] - origin[0]) + 
  100.                              (p[i*3+2] - origin[1]) * (p[i*3+2] - origin[1]));
  101.                     p[i*3+1] += amplitude *
  102.                         easyCos(frequency * d - fraction);
  103. //                        (float)java.lang.Math.cos(frequency * d - fraction);
  104.                 }
  105.                 break;
  106.               case 1:
  107.                 for (i=0; i<n; ++i) {
  108.                     d = p[i*3] * direction[0] + p[i*3+2] * direction[1];
  109.                     p[i*3+1] += amplitude *
  110.                         easyCos(frequency * d - fraction);
  111. //                        (float)java.lang.Math.cos(frequency * d - fraction);
  112.                 }
  113.                 break;
  114.             }
  115.             
  116.             // set calculated positions to vertices of coord
  117.             point.setValue(n*3,p);
  118.         }
  119.     }
  120.  
  121.     // simple cosine function by using linear interpolation
  122.     float easyCos(float x) {
  123.         float yi, yj;
  124.  
  125.         // map x between 0.0f and PI
  126.         if (x < 0.0f) {
  127.             x = -x;
  128.         }
  129.         x -= (int)(x / (2.0f * PI)) * (2.0f * PI);
  130.         if (PI <= x) {
  131.             x = (2.0f * PI) - x;
  132.         }
  133.  
  134.         // linear interpolation
  135.         if (x < 0.2f*PI) {
  136.             x = x / (0.2f * PI);
  137.             yi = 1.0f;        // cos(0)
  138.             yj = 0.809f;    // cos(0.2*PI)
  139.         } else if (x < 0.4f*PI) {
  140.             x = (x - 0.2f * PI) / (0.2f * PI);
  141.             yi = 0.809f;    // cos(0.2*PI)
  142.             yj = 0.309f;    // cos(0.4*PI)
  143.         } else if (x < 0.6f*PI) {
  144.             x = (x - 0.4f * PI) / (0.2f * PI);
  145.             yi = 0.309f;    // cos(0.4*PI)
  146.             yj = -0.309f;    // cos(0.6*PI)
  147.         } else if (x < 0.8f*PI) {
  148.             x = (x - 0.6f * PI) / (0.2f * PI);
  149.             yi = -0.309f;    // cos(0.6*PI)
  150.             yj = -0.809f;    // cos(0.8*PI)
  151.         } else {
  152.             x = (x - 0.8f * PI) / (0.2f * PI);
  153.             yi = -0.809f;    // cos(0.8*PI)
  154.             yj = -1.0f;        // cos(PI)
  155.         }
  156.         return ((1.0f - x) * yi + x * yj);
  157.     }
  158. }
  159.